package com.survey.action;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.survey.bean.Ques;
import com.survey.bean.QuesItem;
import com.survey.bean.QuesItemList;
import com.survey.bean.QuesItemListOption;
import com.survey.bean.QuesItemListOptionAnswer;
import com.survey.bean.QuesUser;
import com.survey.util.Constant;
import com.survey.util.FieldUtil;
import com.survey.util.MapUtil;
import com.survey.util.MessageUtil;
import com.survey.util.Page;
import com.survey.util.common.BaseAction;
import com.survey.service.QuesItemListOptionAnswerService;
import com.survey.service.QuesItemListOptionService;
import com.survey.service.QuesItemListService;
import com.survey.service.QuesItemService;
import com.survey.service.QuesService;
import com.survey.service.QuesUserService;

@Controller
@RequestMapping("/sys")
public class QuesAction extends BaseAction {
	private String													actionname	= "问卷主题";
	private String													actionclass	= "Ques";
	@Autowired
	private QuesService											service;
	@Autowired
	private QuesItemService									quesItemService;
	@Autowired
	private QuesItemListService							quesItemListService;
	@Autowired
	private QuesItemListOptionService				quesItemListOptionService;

	@Autowired
	private QuesUserService									quesUserService;

	@Autowired
	private QuesItemListOptionAnswerService	quesItemListOptionAnswerService;

	@RequestMapping(value = "/add2Ques.do")
	public String add2() {
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addQues";
	}

	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/getQuesPre.do")
	public String getQuesPre(int uid) {
		try {
			Ques temp = service.get(uid);

			request.setAttribute("modifybean", temp);

			List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", uid), "id");
			for (QuesItem item : ilist) {
				List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class, MapUtil.createMap("itemId", item.getId()), "id");
				item.setList(llist);
				for (QuesItemList itemList : llist) {
					List olist = quesItemListOptionService.selectAll(QuesItemListOption.class, MapUtil.createMap("quesItemListId", itemList.getId()), "id");
					itemList.setList(olist);
				}
			}

			putRequestValue("list", ilist);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "getQuesPre";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	//投票分析
	@RequestMapping(value = "/anlQues.do")
	public String anlQues(int uid) {
		try {
			Ques temp = service.get(uid);
			request.setAttribute("modifybean", temp);

			List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", uid), "id");
			for (QuesItem item : ilist) {
				List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class, MapUtil.createMap("itemId", item.getId()), "id");
				item.setList(llist);
				for (QuesItemList itemList : llist) {

					if (itemList.getTypesel().equals("单选") || itemList.getTypesel().equals("多选")) {
						int allsize = 0;
						List<QuesItemListOption> olist = quesItemListOptionService.selectAll(QuesItemListOption.class,
								MapUtil.createMap("quesItemListId", itemList.getId()), "id");
						itemList.setList(olist);
						JSONArray nams = new JSONArray();
						JSONArray values = new JSONArray();
						for (QuesItemListOption o : olist) {
							allsize += o.getAmount();
							nams.add(o.getOptions());
							values.add(o.getAmount());
						}
						for (QuesItemListOption o : olist) {
							o.setVotesize(allsize);
						}
						itemList.setAnlnames(nams.toJSONString());
						itemList.setAnlvalues(values.toJSONString());

					} else if (itemList.getTypesel().equals("填空") || itemList.getTypesel().equals("文本")) {
						List<QuesItemListOptionAnswer> list = quesItemListOptionAnswerService.selectAll(QuesItemListOptionAnswer.class,
								MapUtil.createMap("itemListId", itemList.getId()));
						itemList.setList(list);
					}
				}
			}

			putRequestValue("list", ilist);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "anlQues";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/getQues.do")
	public String get(int uid) {
		try {
			Ques temp = service.get(uid);
			request.setAttribute("modifybean", temp);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyQues";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addQues.do")
	public String add(Ques bean) {
		try {
			bean.setCanyu(0);
			bean.setQstatus("未发布");
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateQues.do")
	public String update(Ques bean) {
		try {
			bean.setImgFile(getUpdateFileField("imgFile"));
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteQues.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQues.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Ques.class, parmnames, parmvalues);

			Page page = service.selectPage(p, Ques.class);

			List<Ques> list = (List<Ques>) page.getList();
			for (Ques ques : list) {
				List<?> ulist = quesUserService.selectAll(QuesUser.class, MapUtil.createMap("quesId", ques.getId()));
				ques.setCanyu(ulist.size());
			}

			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listQues";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQuesAll.do")
	public String queryQuesAll() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Ques.class, parmnames, parmvalues);

			parmnames.add("qstatus");
			parmvalues.add("已发布");

			Page page = service.selectPage(p, Ques.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "queryQuesAll";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQuesAllOfAdmin.do")
	public String queryQuesAllOfAdmin() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Ques.class, parmnames, parmvalues);

			Page page = service.selectPage(p, Ques.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "queryQuesAllOfAdmin";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
